<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>登录</title>
</head>
<body>
<div>
    <label>
        用户名<input type="text" id="textUsername">
    </label>
    <label>
        密码<input type="password" id="textPassword">
    </label>
    <button id="btnLogin">登录</button>
</div>
<script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script>
<script>
    /*发送post请求*/
    function post(url, data = {}) {
        return $.ajax({
            type: 'post',
            url,
            data: JSON.stringify(data),
            contentType: 'application/json'
        })
    }

    /*登录*/
    $('#btnLogin').click(() => {
        const username = $('#textUsername').val();
        const password = $('#textPassword').val();
        const url = '/api/user/login';
        const data = {username, password};
        post(url, data).then(res => {
            if (res.errno !== 0) {
                /*登录失败*/
                alert(res.message);
            } else {
                location.href = './admin.html'
            }
        })
    });
</script>
</body>
</html>